<?php

class Usuarios extends Onecore_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("usuarios_model");
        $this->_moduleName = utf8_encode("Modulo usuarios_model");
        $this->_moduleDescription = utf8_encode("Description") . $this->_moduleName;
        $this->_modelInstance = $this->usuarios_model;
    }

    public function add() {
        $success = false;
        $this->form_validation->set_rules($this->_modelInstance->getValidations());
        $itemName = $this->_modelInstance->getItemName();
        $mensaje = $itemName . " ingresado en forma exitosa";
        $errorList = array();
        $edit = false;

        if ($this->form_validation->run() == FALSE) {
            $mensaje = "Campos inválidos";
            $errorList = $this->form_validation->_error_array;
        } else {
            $this->db->trans_begin();
            $data = array(
                "id" => "",
                "data" => $this->getInputsToSave($this->input->post(), $this->_modelInstance->getFields())
            );
            if ($this->_modelInstance->save($data, false)) {
                $success = true;
            } else {
                $mensaje = "El " . $itemName . " no pudo ser guardado. Verifique los siguientes errores.";
                $errorList = array("Error" => $this->_modelInstance->getError());
            }
            if ($success) {
                $this->db->trans_commit();
            } else {
                $this->db->trans_rollback();
            }
        }
        $res = array(
            "success" => $success,
            "msg" => $mensaje,
            "errors" => $errorList
        );
        $this->layout->printLike(json_encode($res), "json");
    }

    public function edit() {
        $success = false;
        $this->form_validation->set_rules($this->_modelInstance->getValidations());
        $itemName = $this->_modelInstance->getItemName();
        $mensaje = $itemName . " editado en forma exitosa";
        $errorList = array();
        $edit = false;

        if ($this->form_validation->run() == FALSE) {
            $mensaje = "Campos inválidos";
            $errorList = $this->form_validation->_error_array;
        } else {
            $this->db->trans_begin();
            $data = array(
                //   "id"=>$this->getFilterFieldsToEdit($this->input->post(), $this->_modelInstance->getFields()),
                'id' => array(
                    'id_usuario' => $this->input->post('_id_usuario')
                ),
                "data" => $this->getInputsToSave($this->input->post(), $this->_modelInstance->getFields())
            );
            if ($this->_modelInstance->save($data, true)) {
                $success = true;
            } else {
                $mensaje = "El " . $itemName . " no pudo ser guardado. Verifique los siguientes errores.";
                $errorList = array("Error" => $this->_modelInstance->getError());
            }
            if ($success) {
                $this->db->trans_commit();
            } else {
                $this->db->trans_rollback();
            }
        }
        $res = array(
            "success" => $success,
            "msg" => $mensaje,
            "errors" => $errorList
        );
        $this->layout->printLike(json_encode($res), "json");
    }

    public function del() {
        $itemName = $this->_modelInstance->getItemName();
        $success = false;
        $mensaje = "El " . $itemName . " seleccionado no pudo ser eliminado";
        $errorList = array();
        $data = array(
            "id" => $this->getInputsToSave($this->input->post(), $this->_modelInstance->getFields())
        );
        $this->db->trans_begin();
        if ($this->_modelInstance->del($data)) {
            $success = true;
        } else {
            $errorList = array("Error" => $this->_modelInstance->getError());
        }
        if ($success) {
            $mensaje = $itemName . " eliminado en forma exitosa";
            $this->db->trans_commit();
        } else {
            $this->db->trans_rollback();
        }
        $res = array(
            "success" => $success,
            "msg" => $mensaje,
            "errors" => $errorList
        );
        $this->layout->printLike(json_encode($res), "json");
    }

    public function index() {
        $banner["module_name"] = $this->_moduleName;
        $banner["module_description"] = $this->_moduleDescription;
        $data["module_banner"] = $this->load->view("/include/sections/module_banner/module_title", $banner, true);
        $data["page_title"] = "Administración de Usuarios";
        $this->layout->view("/usuarios/index", $data);
    }

}

